package com.freemalll.system.controller;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.freemalll.common.core.utils.StringUtils;
import com.freemalll.common.core.utils.poi.ExcelUtil;
import com.freemalll.common.core.web.controller.BaseController;
import com.freemalll.common.core.web.domain.AjaxResult;
import com.freemalll.common.core.web.page.TableDataInfo;
import com.freemalll.common.log.annotation.Log;
import com.freemalll.common.log.enums.BusinessType;
import com.freemalll.common.security.annotation.RequiresPermissions;
import com.freemalll.common.security.utils.SecurityUtils;
import com.freemalll.system.domain.SysAccount;
import com.freemalll.system.service.ISysAccountService;

/**
 * 系统账户 信息操作处理
 * 
 * @author freemalll
 */
@RestController
@RequestMapping("/account")
public class SysAccountController extends BaseController
{
    @Autowired
    private ISysAccountService accountService;

    /**
     * 获取系统账户列表
     */
    @RequiresPermissions("system:account:list")
    @GetMapping("/list")
    public TableDataInfo list(SysAccount account)
    {
        startPage();
        List<SysAccount> list = accountService.selectAccountList(account);
        return getDataTable(list);
    }

    @Log(title = "系统账户", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:account:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysAccount account)
    {
        List<SysAccount> list = accountService.selectAccountList(account);
        ExcelUtil<SysAccount> util = new ExcelUtil<SysAccount>(SysAccount.class);
        util.exportExcel(response, list, "系统账户数据");
    }

    /**
     * 根据账户编号获取详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable String id)
    {
        return success(accountService.selectAccountById(id));
    }

    /**
     * 根据登录名称查询账户信息
     */
    @GetMapping(value = "/accountId/{accountId}")
    public AjaxResult getInfoByAccountId(@PathVariable String accountId)
    {
        return success(accountService.selectAccountByAccountId(accountId));
    }

    /**
     * 根据手机号查询账户信息
     */
    @GetMapping(value = "/mobile/{mobile}")
    public AjaxResult getInfoByMobile(@PathVariable String mobile)
    {
        return success(accountService.selectAccountByMobile(mobile));
    }

    /**
     * 根据身份证号查询账户信息
     */
    @GetMapping(value = "/idCard/{idCard}")
    public AjaxResult getInfoByIdCard(@PathVariable String idCard)
    {
        return success(accountService.selectAccountByIdCard(idCard));
    }

    /**
     * 根据唯一标识查询账户信息
     */
    @GetMapping(value = "/unionid/{unionid}")
    public AjaxResult getInfoByUnionid(@PathVariable String unionid)
    {
        return success(accountService.selectAccountByUnionid(unionid));
    }

    /**
     * 新增系统账户
     */
    @RequiresPermissions("system:account:add")
    @Log(title = "系统账户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysAccount account)
    {
        if (!accountService.checkAccountIdUnique(account))
        {
            return error("新增账户'" + account.getAccountName() + "'失败，登录名称已存在");
        }
        if (StringUtils.isNotEmpty(account.getMobile()) && !accountService.checkMobileUnique(account))
        {
            return error("新增账户'" + account.getAccountName() + "'失败，手机号已存在");
        }
        if (StringUtils.isNotEmpty(account.getIdCard()) && !accountService.checkIdCardUnique(account))
        {
            return error("新增账户'" + account.getAccountName() + "'失败，身份证号已存在");
        }
        if (StringUtils.isNotEmpty(account.getUnionid()) && !accountService.checkUnionidUnique(account))
        {
            return error("新增账户'" + account.getAccountName() + "'失败，唯一标识已存在");
        }
        account.setCreateName(SecurityUtils.getUsername());
        return toAjax(accountService.insertAccount(account));
    }

    /**
     * 修改系统账户
     */
    @RequiresPermissions("system:account:edit")
    @Log(title = "系统账户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysAccount account)
    {
        if (!accountService.checkAccountIdUnique(account))
        {
            return error("修改账户'" + account.getAccountName() + "'失败，登录名称已存在");
        }
        if (StringUtils.isNotEmpty(account.getMobile()) && !accountService.checkMobileUnique(account))
        {
            return error("修改账户'" + account.getAccountName() + "'失败，手机号已存在");
        }
        if (StringUtils.isNotEmpty(account.getIdCard()) && !accountService.checkIdCardUnique(account))
        {
            return error("修改账户'" + account.getAccountName() + "'失败，身份证号已存在");
        }
        if (StringUtils.isNotEmpty(account.getUnionid()) && !accountService.checkUnionidUnique(account))
        {
            return error("修改账户'" + account.getAccountName() + "'失败，唯一标识已存在");
        }
        account.setUpdateName(SecurityUtils.getUsername());
        return toAjax(accountService.updateAccount(account));
    }

    /**
     * 删除系统账户
     */
    @RequiresPermissions("system:account:remove")
    @Log(title = "系统账户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        accountService.deleteAccountByIds(ids);
        return success();
    }

    /**
     * 重置账户密码
     */
    @RequiresPermissions("system:account:resetPwd")
    @Log(title = "系统账户", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPwd")
    public AjaxResult resetPwd(@RequestBody SysAccount account)
    {
        account.setUpdateName(SecurityUtils.getUsername());
        return toAjax(accountService.resetAccountPassword(account));
    }

    /**
     * 修改账户状态
     */
    @RequiresPermissions("system:account:edit")
    @Log(title = "系统账户", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody SysAccount account)
    {
        account.setUpdateName(SecurityUtils.getUsername());
        return toAjax(accountService.changeAccountStatus(account));
    }

    /**
     * 锁定/解锁账户
     */
    @RequiresPermissions("system:account:edit")
    @Log(title = "系统账户", businessType = BusinessType.UPDATE)
    @PutMapping("/lock")
    public AjaxResult lock(@RequestBody SysAccount account)
    {
        account.setUpdateName(SecurityUtils.getUsername());
        return toAjax(accountService.lockAccount(account));
    }

    /**
     * 增加异常次数
     */
    @Log(title = "系统账户", businessType = BusinessType.UPDATE)
    @PutMapping("/increaseAbnormalCount/{accountId}")
    public AjaxResult increaseAbnormalCount(@PathVariable String accountId)
    {
        return toAjax(accountService.increaseAbnormalCount(accountId));
    }

    /**
     * 重置异常次数
     */
    @RequiresPermissions("system:account:edit")
    @Log(title = "系统账户", businessType = BusinessType.UPDATE)
    @PutMapping("/resetAbnormalCount/{accountId}")
    public AjaxResult resetAbnormalCount(@PathVariable String accountId)
    {
        return toAjax(accountService.resetAbnormalCount(accountId));
    }
}